package com.pace.service.impl;


import com.pace.service.IImg2Xls;
import com.pace.vo.OrderExcel;

import java.util.ArrayList;
import java.util.List;

public class DemoImg implements IImg2Xls {

    @Override
    public List<OrderExcel> toX3Order(String[] arrLine) {
        List<OrderExcel> orderExcels = new ArrayList<>();
        String title = "";
        int num = 0;
        for (int i = 0; i < arrLine.length; i++) {
            if (arrLine[i].endsWith("#")) {
                num = i;
                break;
            }
            title += arrLine[i] + " ";

        }

        for (int i = num; i < arrLine.length; i++) {
            if (arrLine[i].endsWith("#")) {
                String wholeLine = arrLine[i];
                int off = 1;
                String nextLine = arrLine[i + off].trim();
                while (!(nextLine.endsWith("#"))) {
                    if (nextLine.contains("第") || nextLine.contains("页")) {
                        break;
                    }
                    wholeLine = (wholeLine + " " + arrLine[i + off]).trim();
                    off++;
                    nextLine = arrLine[i + off].trim();
                }
                i = i + off - 1;//向下位移
                num++;
                orderExcels.add(getOrderExcel(wholeLine));
            }
        }
        return orderExcels;
    }

    private static OrderExcel getOrderExcel(String wholeLine) {
        OrderExcel orderExcel = new OrderExcel();
        String[] arrLine = wholeLine.split("\\s+");
        orderExcel.setOrderCode(arrLine[0].replace("#", ""));
        orderExcel.setColor(arrLine[1]);
        orderExcel.setTanli(arrLine[2]);
        orderExcel.setSpecificDesc(arrLine[3]);

        int length = arrLine.length - 1;
        for (int i = 0; i < arrLine.length; i++) {
            if (arrLine[i].contains("cm")) {
                orderExcel.setMenfu(arrLine[i]);
                if (i != length) {
                    orderExcel.setPrice(arrLine[i + 1].replace("元", "").replace(".由", ""));
                }
                break;
            }
        }

        return orderExcel;
    }
}
